package com.cooldatasoft.web.product.person;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.ContextImage;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.ProductApplication;
import com.cooldatasoft.data.PersonProperty;
import com.cooldatasoft.data.PersonPropertyValue;
import com.cooldatasoft.data.WorkExp;
import com.cooldatasoft.data.WorkExpProperty;
import com.cooldatasoft.data.WorkExpPropertyValue;
import com.cooldatasoft.data.custom.PersonPageData;
import com.cooldatasoft.data.custom.PersonPropertyKeyValue;
import com.cooldatasoft.data.custom.WorkExpPropertyKeyValue;


public class PersonPage extends WebPage {

	private PersonPageData personPageData = new PersonPageData();
	
	public PersonPage(){
		this(18);
	}
	public PersonPage(Integer personId){
		init(personId);
		initPage();
	}
	public PersonPage(PageParameters param){
		Integer personId = (Integer) param.get("personId");
		
		init(personId);		
		initPage();
	}
	private void initPage() {
		MarkupContainer container= new WebMarkupContainer("container");		
		
		MarkupContainer header = new WebMarkupContainer("header");
		ExternalLink companyWebsiteLink = 
			new ExternalLink("companyWebsiteLink","http://www.cooldatasoft.com","Cooldatasoft.com");
		header.add(companyWebsiteLink);
		container.add(header);
		
		MarkupContainer wrapper = new WebMarkupContainer("wrapper");
		MarkupContainer personalInfo = new WebMarkupContainer("personalInfo");
		
		
		ListView listview = new ListView("personalInfoList", new PropertyModel(this,"personPageData.personalDataList")) {
			@Override
			protected void populateItem(ListItem item) {
				Label keyLabel = new Label("key", new PropertyModel(item.getModel(),"personProperty.label"));
				Label valueLabel = new Label("value", new PropertyModel(item.getModel(),"personPropertyValue.value"));
				keyLabel.setRenderBodyOnly(true);
				valueLabel.setRenderBodyOnly(true);				
				item.add(keyLabel);
				item.add(valueLabel);
			}
		};		
		personalInfo.add(listview);
		wrapper.add(personalInfo);
		
		MarkupContainer imageDiv = new WebMarkupContainer("imageDiv");
		
		String imagePath = "test2.jpg"; //default no image path
		String modelImagePath = personPageData.getNewPerson().getImagePath();
		if(modelImagePath != null && modelImagePath.trim().length()>0){
			imagePath = modelImagePath;
		}		 
		ContextImage personImage = new ContextImage("personImage",new Model( "img/person/"+imagePath));
		personImage.setOutputMarkupId(true);
		imageDiv.add(personImage);
		
		
		MarkupContainer workExperienceDiv = new WebMarkupContainer("workExperienceDiv");
		Label workExperienceTitle= new Label("workExperienceTitle","Work Experience");
		
		
		
		ListView workExperienceList = new ListView("workExperienceList", new PropertyModel(this,"personPageData.workExperienceList")) {
		    protected void populateItem(ListItem item) {
		    	
		    	ListView work = new ListView("work", item.getModel()) {
					@Override
					protected void populateItem(ListItem item2) {
						Label keyLabel = new Label("key", new PropertyModel(item2.getModel(),"workExpProperty.label"));
						Label valueLabel = new Label("value", new PropertyModel(item2.getModel(),"workExpPropertyValue.value"));
						keyLabel.setRenderBodyOnly(true);
						valueLabel.setRenderBodyOnly(true);				
						item2.add(keyLabel);
						item2.add(valueLabel);
					}
				};					
				item.add(work);
		    }
		};
		workExperienceDiv.add(workExperienceList);
		workExperienceDiv.add(workExperienceTitle);
		
		container.add(workExperienceDiv);
		container.add(imageDiv);
		container.add(wrapper);
		add(container);
	}

	private void init(Integer personId) {
		personPageData.setNewPerson(ProductApplication.get().getUserServiceProxy().get(personId));
		
		personPageData.setPersonalDataList(new ArrayList<PersonPropertyKeyValue>());
		List<PersonProperty> personPropertyList = ProductApplication.get().getPersonServiceProxy().getPersonPropertyList();
		for (Iterator<PersonProperty> iterator = personPropertyList.iterator(); iterator.hasNext();) {
			PersonProperty tbPersonProperty = iterator.next();
			PersonPropertyValue value = ProductApplication.get().getPersonServiceProxy().getPersonPropertyValue(personId,tbPersonProperty.getId());
			personPageData.getPersonalDataList().add(new PersonPropertyKeyValue(tbPersonProperty,value));
		}
		
		
		personPageData.setWorkExpDataList(new ArrayList<WorkExpPropertyKeyValue>());
		personPageData.setWorkExperienceList(new ArrayList<List<WorkExpPropertyKeyValue>>());
		
		List<WorkExp> workExpList = ProductApplication.get().getWorkServiceProxy().getWorkExpListByPersonId(personId);
		List<WorkExpProperty> workExpPropertyList = ProductApplication.get().getWorkServiceProxy().getWorkExpPropertyList();
		int index=0;
		
		for (Iterator<WorkExp> iterator = workExpList.iterator(); iterator.hasNext();) {
			WorkExp tbWorkExp = iterator.next();
			personPageData.getWorkExperienceList().add(new ArrayList<WorkExpPropertyKeyValue>());
			for (Iterator<WorkExpProperty> iterator2 = workExpPropertyList.iterator(); iterator2.hasNext();) {
				WorkExpProperty tbWorkExpProperty = iterator2.next();
				WorkExpPropertyValue value = ProductApplication.get().getWorkServiceProxy().getWorkExpPropertyValue(tbWorkExp.getId(),tbWorkExpProperty.getId());
				personPageData.getWorkExperienceList().get(index).add(new WorkExpPropertyKeyValue(tbWorkExp,tbWorkExpProperty,value));
			}
			index++;
		}
	}

	public PersonPageData getPersonPageData() {
		return personPageData;
	}

	public void setPersonPageData(PersonPageData personPageData) {
		this.personPageData = personPageData;
	}
	
}
